<?php
	
	class Noticias extends Applib_Db_Table_Abstract
	{
		protected $_primary = 'id_new';
		protected $_name = 'news';
		
		/* Instancia de la Tabla */
        protected static $_instance = null;
		
		/* Obtener la instancia de la tabla de manera estatica
		* @return Zend_Db_Table
		*/
        public static function getInstance()
        {
            if (null === self::$_instance) 
			{
                self::$_instance = new self();
            }
            return self::$_instance;
        }
		
		public static function getNewsRss()
		{
			$table = self::getInstance();
            $select = $table->select();
            $select->from($table);
			$select->where('visible=1');
			$select->order('id_new DESC');
		    $select->limit(5);
			return $table->fetchAll($select);
		}
		
		/* Metodo estatico para obtener las noticias publicadas */
		public static function getPublicadas()
		{
            $table = self::getInstance();
            
            $select = $table->select();
            /* NOTA:  integrityCheck a false para usar JOIN ?? */
            $select->setIntegrityCheck(false);
            $select->from($table);
            $select->joinLeft(
                'comentarios',
                'comentarios.id_new = news.id_new',
                array('num' =>  new Zend_Db_Expr('COUNT(comentarios.id_new)'))
            );
			$select->where('news.visible=1');
            $select->group(
                array(
                    'news.id_new',
                    'news.fecha_new',
                    'news.imagen',
                    'news.titular',
                    'news.intro',
                    'news.cuerpo',
                    'news.enlace_noticia',
                    'news.visible'
                )
            );
            $select->order('news.id_new DESC');
			
            return $table->fetchAll($select);
		}
		
		/* Metodo estatico para obtener TODAS las noticias */
		public static function getNoticias()
		{
            $table = self::getInstance();
            
            $select = $table->select();
            /* NOTA:  integrityCheck a false para usar JOIN ?? */
            $select->setIntegrityCheck(false);
            $select->from($table);
            $select->joinLeft(
                'comentarios',
                'comentarios.id_new = news.id_new',
                array('num' =>  new Zend_Db_Expr('COUNT(comentarios.id_new)'))
            );
            $select->group(
                array(
                    'news.id_new',
                    'news.fecha_new',
                    'news.imagen',
                    'news.titular',
                    'news.intro',
                    'news.cuerpo',
                    'news.enlace_noticia',
                    'news.visible'
                )
            );
            $select->order('news.id_new DESC');
			
            return $table->fetchAll($select);
		}
		
		/* Metodo estatico para obtener una sola noticia concreta */
		public static function getNew($id)
		{
			$table = self::getInstance();
            $select = $table->select();
            $select->from($table);
		    $select->where('id_new = ?', $id);
			return $table->fetchAll($select);
		}
		
		public function updateEstado($id,$estado)
		{
			$table = new Noticias();
			$data = array ('visible'   => $estado);
			$where = 'id_new =' . $id;
			$table->update($data, $where);
		}
	}